package org.ragdollcat.first.controller;

import cn.hutool.core.util.ObjUtil;
import org.ragdollcat.first.common.BaseResponse;
import org.ragdollcat.first.common.ResultUtils;
import org.ragdollcat.first.constants.SpaceUserPermissionConstant;
import org.ragdollcat.first.constants.UserConstant;
import org.ragdollcat.first.exception.ErrorCode;
import org.ragdollcat.first.exception.ThrowUtils;
import org.ragdollcat.first.manager.auth.SaSpaceCheckPermission;
import org.ragdollcat.first.model.annotation.AuthCheck;
import org.ragdollcat.first.model.dto.analyze.*;
import org.ragdollcat.first.model.entity.Space;
import org.ragdollcat.first.model.vo.*;
import org.ragdollcat.first.service.AnalyzeService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/analyze")
public class AnalyzeController {

    @Resource
    private AnalyzeService analyzeService;

    /**
     * 空间大小使用情况分析
     *
     * @param request
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/spaceUsage")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_VIEW)
    public BaseResponse<SpaceUsageAnalyzeResponse> spaceUsageAnalyze(@RequestBody SpaceUsageAnalyzeRequest request,
                                                                     HttpServletRequest httpServletRequest) {
        ThrowUtils.throwIf(ObjUtil.isEmpty(request), ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(analyzeService.spaceUsageAnalyze(request, httpServletRequest));
    }

    /**
     * 空间图片分类分析
     *
     * @param spaceCategoryAnalyzeRequest
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/spaceCategoryAnalyze")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_VIEW)
    public BaseResponse<List<SpaceCategoryAnalyzeResponse>> spaceCategoryAnalyze(@RequestBody SpaceCategoryAnalyzeRequest spaceCategoryAnalyzeRequest,
                                                                                 HttpServletRequest httpServletRequest) {
        ThrowUtils.throwIf(ObjUtil.isEmpty(spaceCategoryAnalyzeRequest), ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(analyzeService.spaceCategoryAnalyze(spaceCategoryAnalyzeRequest, httpServletRequest));
    }

    /**
     * 空间标签分类使用分析
     *
     * @param spaceTagAnalyzeRequest
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/spaceTagAnalyze")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_VIEW)
    public BaseResponse<List<SpaceTagAnalyzeResponse>> spaceTagAnalyze(@RequestBody SpaceTagAnalyzeRequest spaceTagAnalyzeRequest,
                                                                       HttpServletRequest httpServletRequest) {
        ThrowUtils.throwIf(ObjUtil.isEmpty(spaceTagAnalyzeRequest), ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(analyzeService.spaceTagAnalyze(spaceTagAnalyzeRequest, httpServletRequest));
    }

    /**
     * 空间图片大小分析
     *
     * @param spaceSizeAnalyzeRequest
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/spaceSizeAnalyze")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_VIEW)
    public BaseResponse<List<SpaceSizeAnalyzeResponse>> spaceSizeAnalyze(@RequestBody SpaceSizeAnalyzeRequest spaceSizeAnalyzeRequest,
                                                                         HttpServletRequest httpServletRequest) {
        ThrowUtils.throwIf(ObjUtil.isEmpty(spaceSizeAnalyzeRequest), ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(analyzeService.spaceSizeAnalyze(spaceSizeAnalyzeRequest, httpServletRequest));
    }

    /**
     * 用户上传行为分析
     *
     * @param spaceUserAnalyzeRequest
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/spaceUserAnalyze")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_VIEW)
    public BaseResponse<List<SpaceUserAnalyzeResponse>> spaceUserAnalyze(@RequestBody SpaceUserAnalyzeRequest spaceUserAnalyzeRequest,
                                                                         HttpServletRequest httpServletRequest) {
        ThrowUtils.throwIf(ObjUtil.isEmpty(spaceUserAnalyzeRequest), ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(analyzeService.spaceUserAnalyze(spaceUserAnalyzeRequest, httpServletRequest));
    }

    /**
     * 管理员统计占用存储最多的N个空间
     * 只有管理员才能查看
     *
     * @param spaceRankAnalyzeRequest
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/admin/spaceRankAnalyze")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<List<Space>> spaceRankAnalyze(@RequestBody SpaceRankAnalyzeRequest spaceRankAnalyzeRequest,
                                                      HttpServletRequest httpServletRequest) {
        ThrowUtils.throwIf(ObjUtil.isEmpty(spaceRankAnalyzeRequest), ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(analyzeService.spaceRankAnalyze(spaceRankAnalyzeRequest, httpServletRequest));
    }
}
